package me.goldze.mvvmhabit.http.interceptor;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import me.goldze.mvvmhabit.BuildConfig;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class HeaderInterceptor implements Interceptor {
    private Map<String, String> headers;

    public HeaderInterceptor(Map<String, String> headers) {
        this.headers = headers;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl oldUrl = request.url();
        Request.Builder builder = request.newBuilder();
        List<String> headerValues = request.headers("url_name");
        if (headerValues != null && headerValues.size() > 0) {
            builder.removeHeader("url_name");
            String header = headerValues.get(0);
            HttpUrl newBaseUrl = null;
            if ("file".equals(header)) {
                newBaseUrl = HttpUrl.parse("ossuploadfiles.oss-cn-hangzhou.aliyuncs.com");
            }else{
                newBaseUrl = HttpUrl.parse("http://116.62.58.75:85/API/");
            }
            HttpUrl newUrl = oldUrl.newBuilder().scheme(newBaseUrl.scheme())
                    .host(newBaseUrl.host())
                    .port(newBaseUrl.port())
                    .build();
            return chain.proceed(builder.url(newUrl).build());
        }else {
            return chain.proceed(request);
        }
    }
}